/**
 * @author daniele.gili@usi.ch
 * @author blancsv@usi.ch
 */

package ch.usi.inf.pf2;


import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class PanicSimFrameMenu extends JMenuBar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PanicSimMain circuit;
	private PanicSimDrawingArea area;
	private ArrayList partList;

	public PanicSimFrameMenu(final PanicSimMain circuit,
			final PanicSimDrawingArea area) {

		final int SHORTCUT_MASK = Toolkit.getDefaultToolkit()
				.getMenuShortcutKeyMask();

		this.circuit = circuit;
		this.area = area;

		final JMenu menu = new JMenu("File");

		// Define menu items and corresponding actionListeners
		JMenuItem itemLoad = new JMenuItem("Load");
		itemLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
				SHORTCUT_MASK));
		itemLoad.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				LoadFile();
				area.repaint();
			}
		});

		JMenuItem itemSave = new JMenuItem("Save");
		itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
				SHORTCUT_MASK));
		itemSave.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				SaveFile();
				area.repaint();
			}
		});

		JMenuItem itemClose = new JMenuItem("Quit");
		itemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				SHORTCUT_MASK));
		itemClose.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				close();
			}
		});

		JMenuItem itemReset = new JMenuItem("Reset");
		itemReset.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				SHORTCUT_MASK));
		itemReset.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.deleteAll();
			}
		});

		JMenu circuitMenu = new JMenu("Circuit");

		JMenuItem itemRun = new JMenuItem("Compile");
		itemRun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				SHORTCUT_MASK));
		itemRun.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				circuit.startCompile(area);
			}
		});
		
		
		JMenuItem itemClocked = new JMenuItem("clocked");
	//	itemRun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
		//		SHORTCUT_MASK));
		itemClocked.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent ev) {
				area.setClocked();
			}
		});
		
		
		JMenuItem selectAll = new JMenuItem("Select All");
		//	itemRun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
			//		SHORTCUT_MASK));
			selectAll.addActionListener(new ActionListener() {
				public void actionPerformed(final ActionEvent ev) {
					area.setSelectedGate(circuit.getpartList());
				}
			});


		// Add the items to the menu bar - add the menus

		menu.add(itemReset);
		menu.addSeparator();
		menu.add(itemSave);
		menu.add(itemLoad);
		menu.addSeparator();
		menu.add(itemClose);

		circuitMenu.add(itemRun);
		circuitMenu.add(itemClocked);
		circuitMenu.add(selectAll);

		add(menu);
		add(circuitMenu);

	}

	// Saves the actual circuit

	public void SaveFile() {
	

	JFileChooser chooser = new JFileChooser();

	int returnValue = chooser.showSaveDialog(area);

	if (returnValue == JFileChooser.APPROVE_OPTION) {
	File file = chooser.getSelectedFile();
	String path = file.getAbsolutePath();
	FileSaver.save(path, circuit.getpartList());
	}

	}

	// Load an already existing circuit
	public void LoadFile() {


	JFileChooser chooser = new JFileChooser();

	int returnValue = chooser.showOpenDialog(area);

	if (returnValue == JFileChooser.APPROVE_OPTION) {
	File file = chooser.getSelectedFile();
	String path = file.getAbsolutePath();
	circuit.partList = (ArrayList) FileLoader.load(path,"LogicSimMain");
	}
	}
	// Close the application
	public void close() {
		System.exit(0);
	}
}
